<?php
/*
 * @Author: your name
 * @Date: 2021-09-21 16:46:35
 * @LastEditTime: 2022-03-01 13:35:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nlog/app/Http/Controllers/AdminController/Login.php
 */

namespace App\Http\Controllers\crm;

use Illuminate\Support\Facades\Request;
use App\Models\crm_model\CrmDivision as CrmDivisionDb;

class CrmDivision extends BaseController
{
  public function  divisionOptional(){
        //  if(Request::has(''))
        $adminInfo    =  $this->getAdminInfo();
        $division_id  =  $adminInfo['division_id'];  //部门id
        // if($division_id){
        $list =    CrmDivisionDb::divisionList($division_id);
        // }
        return $this->create(200,'查询成功',['list'=>$list]);
  }

  public function divisionList()
  {
    // $limitStr = '';
    // if (Request::has('limit') && Request::has('page')) {
    //     $limitStr = limit_joint(Request::input('page'), Request::input('limit'));
    // }

    $whereStr = '';
    if (Request::has('keyWord')) {
      $keyWord = array_filter(Request::input('keyWord'), 'filtrfunction');
      if (!empty($keyWord)) {
        $whereStr .= havaWhereLikeJoinEqual($keyWord);
      }
    }

    // $list  =  CrmDivisionDb::divisionList( $whereStr, $limitStr);
    // $total =  CrmDivisionDb::divisionCount( $whereStr);

    $list  =   CrmDivisionDb::divisionAll($whereStr);
    return $this->create(200, '查询成功', ['list' =>  $this->tree($list)]);
  }


  
  private function tree($list)
  {
    $keyList = [];
    foreach ($list as $val) {
      $keyList[$val['id']] = $val;
    }
    $newList = [];
    foreach ($keyList  as  &$val) {
      if ($val['pid'] ==  0) {
        $newList[] = &$val;
      } else if (isset($keyList[$val['pid']])) {
        $keyList[$val['pid']]['children'][] =   &$val;
      }
    }
    return $newList;
  }


}
